package com.fyh.mapper;

import com.fyh.entity.community.*;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CommunityMapper {
    /**
     * 获取所有分类
     *
     * @return
     */
    @Select("select * from categories")
    List<Category> getCategoryList();

    /**
     * 发布帖子
     * @param postVO
     */
    @Insert("insert into posts (user_id, category_id, title, content, create_time, update_time) values (#{userId}, #{categoryId}, #{title}, #{content}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyColumn = "post_id", keyProperty = "postId")
    void insertPost(PostVO postVO);

    /**
     * 插入附件路径列表
     * @param postId
     * @param attachmentPathList
     */
    void insertAttachmentPathList(@Param("postId") Long postId, @Param("attachmentPathList") List<String> attachmentPathList);

    /**
     * 获取帖子列表
     * @param postPageDTO
     * @return
     */
    List<PostVO> getPostList(PostPageDTO postPageDTO);

    /**
     * 获取帖子点赞数
     * @param postId
     * @return
     */
    @Select("select count(*) from reaction_record where post_id = #{postId} and reaction_type = 'like'")
    Integer getPostLikeCount(Long postId);

    /**
     * 获取帖子收藏数
     * @param postId
     * @return
     */
    @Select("select count(*) from post_favorite where post_id = #{postId}")
    Integer getPostFavouriteCount(Long postId);

    /**
     * 获取帖子回复数
     * @param postId
     * @return
     */
    @Select("select count(*) from replies where post_id = #{postId}")
    Integer getPostReplyCount(Long postId);

    /**
     * 获取帖子是否被收藏
     * @param userId
     * @param postId
     * @return
     */
    @Select("select exists(select 1 from post_favorite where user_id = #{userId} and post_id = #{postId})")
    Boolean getIsFavourite(@Param("userId") Long userId, @Param("postId") Long postId);

    /**
     * 获取帖子互动类型（点赞like，点踩dislike）
     * @param userId
     * @param postId
     * @return
     */
    String getReactionType(@Param("userId") Long userId, @Param("postId") Long postId, @Param("replyId") Long replyId);

    /**
     * 根据id获取类型名
     * @param categoryId
     * @return
     */
    @Select("select category_name from categories where category_id = #{categoryId}")
    String getCategoryName(Long categoryId);

    /**
     * 删除用户的互动记录
     * @param reactionDTO
     */
    void deleteReactionRecord(ReactionDTO reactionDTO);

    /**
     * 添加用户的互动记录
     * @param reactionDTO
     */
    @Insert("insert into reaction_record (user_id, post_id, reply_id, reaction_type, reaction_time) values (#{userId}, #{postId}, #{replyId}, #{reactionType}, #{reactionTime})")
    void insertReactionRecord(ReactionDTO reactionDTO);

    /**
     * 添加用户的收藏记录
     * @param reactionDTO
     */
    @Insert("insert into post_favorite (user_id, post_id, favorite_time) values (#{userId}, #{postId}, #{reactionTime})")
    void insertFavorite(ReactionDTO reactionDTO);

    /**
     * 删除用户的收藏记录
     * @param reactionDTO
     */
    @Delete("delete from post_favorite where user_id = #{userId} and post_id = #{postId}")
    void deleteFavorite(ReactionDTO reactionDTO);

    /**
     * 获取帖子详情
     * @param postId
     * @return
     */
    @Select("select * from posts where post_id = #{postId}")
    PostVO getPostDetail(Long postId);

    /**
     * 获取帖子附件列表
     * @param postId
     * @return
     */
    @Select("select attachment_path from attachments where post_id = #{postId}")
    List<String> selectAttachmentPathList(Long postId);

    /**
     * 获取帖子点踩数
     * @param postId
     * @return
     */
    @Select("select count(*) from reaction_record where post_id = #{postId} and reaction_type = 'dislike'")
    Integer getPostDislikeCount(Long postId);

    /**
     * 更新帖子热度
     * @param postId
     * @param i
     * @return
     */
    @Update("update posts set heat = #{i} where post_id = #{postId}")
    void updatePostHeat(@Param("postId") Long postId, @Param("i") Integer i);

    /**
     * 获取帖子一级回复列表
     * @param postId
     * @return
     */
    @Select("select * from replies where post_id = #{postId} and parent_id is null order by heat")
    List<ReplyVO> getReplyListByPostId(Long postId);

    /**
     * 获取回复点赞数
     * @param replyId
     * @return
     */
    @Select("select count(*) from reaction_record where reply_id = #{replyId} and reaction_type = 'like'")
    Integer getReplyLikeCount(Long replyId);

    /**
     * 获取回复点踩数
     * @param replyId
     * @return
     */
    @Select("select count(*) from reaction_record where reply_id = #{replyId} and reaction_type = 'dislike'")
    Integer getReplyDislikeCount(Long replyId);

    /**
     * 获取回复回复数
     * @param replyId
     * @return
     */
    @Select("select count(*) from replies where parent_id = #{replyId}")
    Integer getReplyReplyCount(Long replyId);

    /**
     * 更新回复热度
     * @param replyId
     * @param i
     * @return
     */
    @Update("update replies set heat = #{i} where reply_id = #{replyId}")
    void updateReplyHeat(@Param("replyId") Long replyId, @Param("i") int i);

    /**
     * 获取二级回复列表
     * @param replyPageDTO
     * @return
     */
    @Select("select * from replies where parent_id = #{parentId}")
    List<ReplyVO> getReplyReplyList(ReplyPageDTO replyPageDTO);

    /**
     * 添加回复
     * @param replyDTO
     */
    @Insert("insert into replies (user_id, post_id, parent_id, reply_user_id, content, create_time) values (#{userId}, #{postId}, #{parentId}, #{replyUserId}, #{content}, #{createTime})")
    void insertReply(ReplyDTO replyDTO);
}
